package com.yonyougov.devops.cruxcisupport.userserver;

import com.yonyougov.devops.cruxcisupport.user.UserEntity;
import com.yonyougov.devops.cruxcisupport.userserver.vo.*;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.exception.BusinessException;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * t_user_server服务实现
 */
@Service
public class UserServerServiceImpl extends BaseServiceImpl<UserServerEntity, BigInteger> implements UserServerService {
    private UserServerDao userServerDao;

    @Override
    public BaseDao<UserServerEntity, BigInteger> getDao() {
        return userServerDao;
    }

    public UserServerServiceImpl(UserServerDao userServerDao) {
        this.userServerDao = userServerDao;
    }

    @Override
    public int countByUserServerLike(UserServerPageLikeReqVo userServerPageLikeReqVo) {
        return userServerDao.countByUserServerLike(userServerPageLikeReqVo);
    }

    @Override
    public List<UserServerPageLikeRespVo> findUserServerPageLike(UserServerPageLikeReqVo userServerPageLikeReqVo) {
        return userServerDao.findUserServerPageLike(userServerPageLikeReqVo);
    }

    @Override
    public void deleteBatchUserServerByIds(UserEntity user, List<BigInteger> ids) {
        userServerDao.deleteBatchUserServerByIds(user, ids);
    }

    @Override
    public BaseResponseDto addPublicKey(BigInteger userId, List<BigInteger> serverIds) {
        return userServerDao.userAddServer(userId, serverIds);
    }

    @Override
    public int countByUserLinkedServer(ServerLinkedUserPageLikeReqVo reqVo) {
        return userServerDao.countByUserLinkedServer(reqVo);
    }

    @Override
    public List<ServerLinkedUserPageLikeRespVo> findUserLinkedServerPageLike(ServerLinkedUserPageLikeReqVo reqVo) {
        return userServerDao.findUserLinkedServerPageLike(reqVo);
    }

    @Override
    public BaseResponseDto findServerPubKey(BigInteger serverId) {
        return userServerDao.findServerPubKey(serverId);
    }

    @Override
    public void deleteServerPubKey(ServerDeletePubKeyReqVo pubKeyReqVo) throws  BusinessException {
        for (String keys:
             pubKeyReqVo.getPubKey()) {
            if(keys.endsWith("zwcd@yonyou.com")){
                pubKeyReqVo.getPubKey().remove(keys);
                throw new BusinessException("zwcd公钥不可删除");
            }
        }
        if(pubKeyReqVo.getPubKey().size()>0){
            userServerDao.deleteServerPubKey(pubKeyReqVo);
        }
    }

    @Override
    public void printPubKey() {
        userServerDao.printPubKey();
    }

    @Override
    public void execCommand(List<BigInteger> serverId, String command) {
        userServerDao.execCommand(serverId, command);
    }

    @Override
    public void addAll(List<UserServerEntity> needAddUserServers) {
        userServerDao.batchInsert(needAddUserServers);
    }
}
